﻿<Window x:Class="ShellNavigator.Client.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:Models="clr-namespace:ShellNavigator.Client.Models"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        xmlns:Behaviour="clr-namespace:ShellNavigator.Client.Behaviour"
        xmlns:Utils="clr-namespace:ShellNavigator.Client.Utils" Title="Shell Navigator" Height="480" Width="640"
        WindowStartupLocation="CenterScreen" KeyUp="Window_KeyUp" Loaded="Window_Loaded" Closed="Window_Closed"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:Forms="clr-namespace:ShellNavigator.Client.Forms" mc:Ignorable="d">
    <Window.Resources>
        <ObjectDataProvider x:Key="appTargets" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="Models:AppTargets" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        
        <HierarchicalDataTemplate DataType="{x:Type Models:FileInfo}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" Foreground="DarkGreen"></TextBlock>
                <TextBlock Text="    "></TextBlock>
                <TextBlock Text="{Binding TextLine}" Foreground="Gray"></TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type Models:ProjectInfo}" ItemsSource="{Binding Files}">
            <TextBlock Text="{Binding Name}" Foreground="DarkBlue"></TextBlock>
        </HierarchicalDataTemplate>
        
        <HierarchicalDataTemplate DataType="{x:Type Models:SolutionInfo}" ItemsSource="{Binding Projects}">
            <TextBlock Text="{Binding Name}" Foreground="DarkRed"></TextBlock>
            <HierarchicalDataTemplate.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="True"/>
                </Style>
            </HierarchicalDataTemplate.ItemContainerStyle>
        </HierarchicalDataTemplate> 
     
        <Models:MainWindowModel x:Key="model"/>
        <Utils:CountConverter x:Key="countConverter"/>
    </Window.Resources>
    <Grid Margin="5" d:DataContext="{StaticResource ResourceKey=model}">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="20" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="20" />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>

            <Grid Grid.Column="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>

                <Label Grid.Column="0" Grid.Row="0" Target="{Binding ElementName=fileFilter}">File or mask:</Label>
                <ComboBox Grid.Column="1" Grid.Row="0" Height="23" x:Name="fileFilter" IsEditable="True" 
                      Text="{Binding FileOrMask, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"
                      ItemsSource="{Binding FileOrMaskHistory}" />
                <Button Grid.Column="2" Command="{Binding OpenFileDialog}">...</Button>

                <Label Grid.Column="0" Grid.Row="1" Target="{Binding ElementName=directory}">Directory:</Label>
                <ComboBox Grid.Column="1" Grid.Row="1" Height="23" x:Name="directory" IsEditable="True" 
                      Text="{Binding Directory, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"
                      ItemsSource="{Binding DirectoryHistory}" />

                <Button Grid.Column="2" Grid.Row="1" Command="{Binding ReduceDirectory}">..\</Button>

                <Label Grid.Column="0" Grid.Row="2" Target="{Binding ElementName=textFilter}">Text Filter:</Label>
                <ComboBox Grid.Column="1" Grid.Row="2" IsEditable="True" x:Name="textFilter"
                      Text="{Binding TextFilter, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"
                      ItemsSource="{Binding TextFilterHistory}" />
                
                <Label Grid.Column="0" Grid.Row="3" Target="{Binding ElementName=target}">Target:</Label>
                <ComboBox Grid.Column="1" Grid.Row="3" x:Name="target"  
                      SelectedItem="{Binding Target, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"
                      ItemsSource="{Binding Source={StaticResource appTargets}}" />
            </Grid>

            <Grid Grid.Column="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>
                <Label>Text Options</Label>
                <CheckBox Grid.Row="1" IsChecked="{Binding IsMatchCase}">Match case</CheckBox>
                <CheckBox Grid.Row="3" IsChecked="{Binding IsPlainText}">Plain text</CheckBox>
            </Grid>
        </Grid>
        <Grid Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="20" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Button Grid.Column="0" Command="{Binding Find}">Find</Button>

            <StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Command="{Binding CopyFile}">Full path to clipboard</Button>
                <StackPanel Width="10"/>
                <Button Command="{Binding CopyAssembly}">Assembly name to Clipboard</Button>
            </StackPanel>
        </Grid>
        <Grid Grid.Row="3">
            <TreeView x:Name="resultBox" 
                     Forms:TreeViewHelper.Attach="True" Forms:TreeViewHelper.SelectedItem="{Binding Result, Mode=OneWayToSource}"
                     ItemsSource="{Binding SearchResults}"
                     Behaviour:ListBoxBehaviour.MouseDoubleClickCommand="{Binding RunAction}"
                     Behaviour:ListBoxBehaviour.KeyUpCommand="{Binding RunAction}">
            </TreeView>
            <TextBlock 
                HorizontalAlignment="Center" VerticalAlignment="Center"
                Visibility="{Binding SearchResults.Count, Converter={StaticResource countConverter}}">No results found.</TextBlock>
        </Grid>
    </Grid>
</Window>